package com.alan.wifishare.view.impl.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.Parcelable;
import android.util.Log;

import com.alan.wifishare.presenter.wifi.ALNetwork;
import com.alan.wifishare.presenter.wifi.NetworkStateObserverManager;

/**
 * @ClassName:  NetworkManagerService   
 * @Description:  network manage service
 * @author: jajuan.wang  
 * @date:   2015-12-15 12:47  
 * version:1.0.0
 */
public class NetworkManagerService extends Service {
	
	private static String TAG = "NetworkManagerService";
	
	public static String ACTION_START_SERVICE = "action_start_service";
	public static String ACTION_STOP_SERVICE = "action_stop_service";
	
	
	public static final String WIFI_AP_STATE_CHANGED 		= "android.net.wifi.WIFI_AP_STATE_CHANGED";
	public static final String WIFI_STATE_CHANGED 			= "android.net.wifi.WIFI_STATE_CHANGED";
	public static final String NETWORK_STATE_CHANGE 		= "android.net.wifi.STATE_CHANGE";
	public static final String ETHERNET_STATE_CHANGED 		= "android.net.ethernet.ETHERNET_STATE_CHANGED";
	public static final String CONNECTIVITY_CHANGE 			= "android.net.conn.CONNECTIVITY_CHANGE";
	
	
	/**
	 * static method to start service
	 * @param context
	 */
	public static void startService(Context context) {
		Intent intent = new Intent(context, NetworkManagerService.class);
		intent.setAction(ACTION_START_SERVICE);
		context.startService(intent);
	}
	
	/**
	 * static method to stop service
	 * @param context
	 */
	public static void stopService(Context context) {
		Intent intent = new Intent(context, NetworkManagerService.class);
		intent.setAction(ACTION_STOP_SERVICE);
		context.startService(intent);
	}
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStartCommand(intent, flags, startId);
		Log.i(TAG, "intent.getAction()="+intent.getAction());
		// option based on action
		if (intent.getAction().equals(ACTION_START_SERVICE) == true) {
			//startService();do on {@link #onCreat()}
		} else if (intent.getAction().equals(ACTION_STOP_SERVICE) == true) {
			stopSelf();
		} 
		//重传Intent。使用这个返回值时，如果在执行完onStartCommand后，服务被异常kill掉，系统会自动重启该服务，并将Intent的值传入。
		return Service.START_REDELIVER_INTENT;
	}
	
	@Override
	public void onCreate() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(WIFI_AP_STATE_CHANGED);
		intentFilter.addAction(WIFI_STATE_CHANGED);
		intentFilter.addAction(ETHERNET_STATE_CHANGED);
		intentFilter.addAction(CONNECTIVITY_CHANGE);
		intentFilter.addAction(NETWORK_STATE_CHANGE);
		registerReceiver(networkReceiver,intentFilter);
		super.onCreate();
	}
	
	
	
	@Override
	public void onDestroy() {
		unregisterReceiver(networkReceiver);
		super.onDestroy();
	}
	


	private BroadcastReceiver networkReceiver = new BroadcastReceiver(){
        public void onReceive(Context context, Intent intent) {
        	String action = intent.getAction();
        	Log.d(TAG, " action =  " + action);  
        	
        	
			 
        	if (action.equals(WIFI_AP_STATE_CHANGED)) {
        		int state = intent.getIntExtra("wifi_state",  0);
                Log.e(TAG, "WIFI_AP_STATE = "+state);
                NetworkStateObserverManager.getInstance().onStatusReport(ALNetwork.Type.AP, state);
                
			}else if (action.equals(NETWORK_STATE_CHANGE)) {
				Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        		if (null != parcelableExtra) {
        			NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
        			Log.d(TAG, " intent is getState = " + networkInfo.getState()); 
        		}
			}else if (action.equals(WIFI_STATE_CHANGED)) {
        		int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0); 
                Log.e(TAG, "WIFI_STATE = " + wifiState);  
                NetworkStateObserverManager.getInstance().onStatusReport(ALNetwork.Type.WIFI, wifiState);
                
			}else if (action.equals(ETHERNET_STATE_CHANGED)) {
//				int state = EthernetUtil.getInstance(NetworkManagerService.this).getEtherState();
//				Log.e(TAG, "ETHERNET_STATE = " + state);
//				NetworkStateObserverManager.getInstance().onStatusReport(ALNetwork.Type.ETHERNET, state);
				
			}else if (action.equals(CONNECTIVITY_CHANGE)) {
				
				NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra("networkInfo");
				if (networkInfo != null) {
					NetworkInfo.DetailedState detailedState = networkInfo.getDetailedState();
					switch (detailedState) {
					case DISCONNECTED:
						NetworkStateObserverManager.getInstance().onStatusReport(ALNetwork.Type.CONNECTIVITY, ALNetwork.State.CONNECTIVITY_DISCONNECTED);
						break;
					case CONNECTED:
						NetworkStateObserverManager.getInstance().onStatusReport(ALNetwork.Type.CONNECTIVITY, ALNetwork.State.CONNECTIVITY_CONNECTED);
						
						break;

					default:
						break;
					}
				}
				
			}
        	
        }
	};

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
